import { ISourceIntentionAPI } from '../common/type';
import { Button, Tooltip } from 'antd';
import { getPosIDs, getTenantID, getUserID, removeDateHmsStr, tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { SHTH_CHECKSTATE, SHTG_CHECKSTATE } from '../../Acommon';
import { EditOutlined, TagOutlined, CloseCircleOutlined } from '@ant-design/icons';
import { ColumnsRenderFun, DictionaryRenderFun, ILoadMapTypeAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import moment from 'moment';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: ISourceIntentionAPI.ISourceTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];
  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible:
        curRes?.['/purchaseSourceIntentController/edit'] &&
        ((record?.state == `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1000` && !record?.checkState) ||
          (record?.state == `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1001` && record?.checkState == SHTH_CHECKSTATE())) &&
        record?.applyerID == getUserID() &&
        record?.applyPositionID == getPosIDs(),
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onInvalid',
    //   isVisible:
    //     curRes['/purchaseSourceController/invalid'] &&
    //     record?.id &&
    //     record?.applyerID == getUserID() &&
    //     record?.applyPositionID == getPosIDs() &&
    //     (record?.state == `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1001` ||
    //       (record?.state == `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1000` && record?.checkState == SHTH_CHECKSTATE())),
    // },
    {
      icon: <TagOutlined />,
      btnName: '公布',
      clickFnc: operateFuns,
      clickType: 'onPublish',
      isVisible:
        curRes['/purchaseSourceIntentController/intentPublishTime'] &&
        record?.id &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1004` &&
        record?.checkState == SHTG_CHECKSTATE(),
    },
    {
      icon: <CloseCircleOutlined />,
      btnName: '作废',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible:
        curRes?.['/purchaseSourceIntentController/cancel'] &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1000` &&
        !record?.checkState &&
        !record?.packID,
    },
  ];

  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);

// Table表格的配置
const getTableColumns = (curRes: Record<string, any>, operateFuns: ISourceIntentionAPI.ISourceTableActionType) => {
  /** 日期去除时分秒 */
  const EstimatedTimeOfPurchase = (dateStr: string) => {
    if (dateStr) {
      const timeStr: string = moment(dateStr)?.format('YYYY-MM');
      return timeStr;
    } else {
      return '--';
    }
  };
  return [
    {
      title: '申请人部门', // 原【申请部门】
      dataIndex: 'createDepartmentName', // own
      key: 'createDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.createDepartmentName, b?.createDepartmentName),
      width: 200,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '意向公开名称',
      dataIndex: 'intentName',
      sorter: (a: any, b: any) => getSorter(a?.intentName, b?.intentName),
      key: 'intentName',
      visible: true,
      width: 200,
      render: (text: string, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              if (curRes?.['/purchaseSourceIntentController/getIntentById/{id}/{currentUserID}']) {
                operateFuns?.onView(record);
              }
            }}
            style={{ width: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '采购需求概述',
      dataIndex: 'purchaseInfo',
      key: 'purchaseInfo',
      width: 220,
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.purchaseInfo, b?.purchaseInfo),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '备注',
      dataIndex: 'remark',
      key: 'remark',
      width: 220,
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.remark, b?.remark),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      width: 200,
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.state, b?.state),
      render: (text: string | null) => DictionaryRenderFun({ text, isState: true }),
    },

    {
      title: '意向公开时间',
      dataIndex: 'intentTime',
      key: 'intentTime',
      sorter: (a: any, b: any) => getSorter(a?.intentTime, b?.intentTime),
      width: 200,
      visible: true,
      render: (text: string) => <span>{text ? removeDateHmsStr(text) : '--'}</span>,
    },
    {
      title: '拟公告时间',
      dataIndex: 'announcementTime',
      key: 'announcementTime',
      sorter: (a: any, b: any) => getSorter(a?.announcementTime, b?.announcementTime),
      width: 200,
      visible: true,
      render: (text: string) => <span>{text ? removeDateHmsStr(text) : '--'}</span>,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: (a: any, b: any) => getSorter(a?.createTime, b?.createTime),
      width: 200,
      visible: true,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '创建人公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      sorter: (a: any, b: any) => getSorter(a?.createCompanyName, b?.createCompanyName),
      width: 200,
      visible: false,
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },

    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      sorter: (a: any, b: any) => getSorter(a?.createDepartmentName, b?.createDepartmentName),
      width: 200,
      visible: true,
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      sorter: (a: any, b: any) => getSorter(a?.createPositionName, b?.createPositionName),
      width: 200,
      visible: false,
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      sorter: (a: any, b: any) => getSorter(a?.createrID, b?.createrID),
      width: 200,
      visible: true,
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '联系电话',
      dataIndex: 'applyPhone',
      key: 'applyPhone',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.applyPhone, b?.applyPhone),
      width: 200,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '申请时间',
      dataIndex: 'applyTime',
      key: 'applyTime',
      visible: false,
      width: 200,
      sorter: (a: any, b: any) => getSorter(a?.applyTime, b?.applyTime),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '申请人公司',
      dataIndex: 'applyCompanyName',
      key: 'applyCompanyName',
      width: 200,
      visible: false,
      sorter: (a: any, b: any) => getSorter(a?.applyCompanyID, b?.applyCompanyID),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人部门',
      dataIndex: 'applyDepartmentName',
      key: 'applyDepartmentName',
      width: 200,
      visible: false,
      sorter: (a: any, b: any) => getSorter(a?.applyDepartmentID, b?.applyDepartmentID),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人岗位',
      dataIndex: 'applyPositionName',
      key: 'applyPositionName',
      width: 200,
      visible: false,
      sorter: (a: any, b: any) => getSorter(a?.applyPositionID, b?.applyPositionID),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '申请人',
      dataIndex: 'applyerName',
      key: 'applyerName',
      width: 200,
      visible: false,
      sorter: (a: any, b: any) => getSorter(a?.applyerID, b?.applyerID),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 200,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: any, record: Record<string, any>) => {
        return Operation(curRes, operateFuns, record);
      },
    },
  ];
};

export { getTableColumns };
